﻿import { Component, EventEmitter, Injector, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { PasswordComplexitySetting, ProfileServiceProxy, UserEditDto, UserServiceProxy } from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';
import { RequirementProductionServiceProxy,RequirementChangeHistoryDto, RoleUserServiceProxy,RequirementPackageDto } from '@shared/service-proxies/service-proxies';
import { StandardPartsDetailServiceProxy,RequirementForModelDto} from '@shared/service-proxies/service-proxies';
@Component({
    selector: 'requirementManagerItemEditComponent',
    templateUrl: './requirement-manager-item_edit.component.html',
    styleUrls: ['./requirement-manager-item_edit.component.less']

})
export class RequirementManagerItemEditComponent extends AppComponentBase {
    @ViewChild('createOrEditModal') modal: ModalDirective;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    active = false;
    saving = false;
    passwordComplexitySetting: PasswordComplexitySetting = new PasswordComplexitySetting();
    formData: RequirementForModelDto = new RequirementForModelDto();
    user: UserEditDto = new UserEditDto();
    passwordComplexityInfo = '';
    profilePicture: string;
    planDate:Date = new Date();
    rquirementPackageDto: RequirementPackageDto;
    // 标准件编码字段选择
    // 标准件
    // 全部标准件
    standardPartsDetailAll:any[];
    variationValue:number;
    reason:string;
    userName:string;
    authorityUserOption:Array<object>;
    // 筛选标准件

    rqeusetStandardPartsDetailAll(): void {
        this.primengTableHelper.showLoadingIndicator();
        this._standardPartsDetailServiceProxy.getAllDetail("", null, 1000, 0)
            .pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
                this.standardPartsDetailAll = result.items;
            });
    }
    
    constructor(
        injector: Injector,
        private _userService: UserServiceProxy,
        private _pRequirementProductionServiceProxy: RequirementProductionServiceProxy,
        private _roleUserServiceProxy:RoleUserServiceProxy,
        private _standardPartsDetailServiceProxy: StandardPartsDetailServiceProxy,
        private _profileService: ProfileServiceProxy
    ) {
        super(injector);
        this.getAuthorityUserOption();
    }
        /**
     * 获取有权限的用户
     */
    getAuthorityUserOption() {
        this.authorityUserOption = new Array<object>()
        // 车间审批领导
        this._roleUserServiceProxy.getUsersByRole(0)
            .pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
                this.authorityUserOption = result;
            });
    }
    show(requirementForModelDto: RequirementForModelDto, userName:string): void {
        if (requirementForModelDto) {
            this.formData = requirementForModelDto;
            this.userName = userName;
        }
        this.active = true;
        this.modal.show();
        this.rqeusetStandardPartsDetailAll();
    }

    onShown(): void {
        
    }
    /**
     * 需求详细添加保存
     * 需求详细编辑保存
     */
    save(): void {
        this.saving = true;
        let param : RequirementChangeHistoryDto  = new RequirementChangeHistoryDto();
        param.variationValue = this.variationValue;
        param.reason = this.reason;
        param.requirementForModelId = this.formData.id;
        param.changer =this.userName;
        // 日期转换
        this._pRequirementProductionServiceProxy.createRequirementChange(param)
            .pipe(finalize(() => { this.saving = false; }))
            .subscribe(result => {
                this.submitRequirementChange(result);
            });
    }

    submitRequirementChange(_pId: number): void {
        // 日期转换
        this._pRequirementProductionServiceProxy.submitRequirementChange(_pId)
            .pipe(finalize(() => { this.saving = false; }))
            .subscribe(result => {
                this.notify.info(this.l('SavedSuccessfully'));
                this.close();
                this.modalSave.emit(null);
            });
    }

    close(): void {
        this.active = false;
        this.modal.hide();
    }
}
